home *** CD-ROM | disk | FTP | other *** search
/ Aminet 34 / Aminet 34 (2000)(Schatztruhe)[!][Dec 1999].iso / Aminet / util / misc / cycledbase.lha / CycleDBase / AdvSearch / Search.c < prev   
Encoding:
C/C++ Source or Header  |  1999-09-27  |  12.9 KB  |  421 lines

  1. /* Search.c */
  2.  
  3. // File contains all search routines for Cycle database
  4.  
  5. #include <exec/types.h>
  6. #include <exec/memory.h>
  7. #include <clib/asl_protos.h>
  8. #include <clib/dos_protos.h>
  9. #include <clib/exec_protos.h>
  10. #include <clib/intuition_protos.h>
  11. #include <clib/gadtools_protos.h>
  12. #include <clib/graphics_protos.h>
  13. #include <graphics/gfxmacros.h>
  14. #include <graphics/GfxBase.h>
  15. #include <intuition/intuition.h>
  16. #include <intuition/IntuitionBase.h>
  17. #include <libraries/asl.h>
  18. #include <libraries/dos.h>
  19. #include <libraries/gadtools.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23.  
  24. #include "ResultsWindow.c"
  25. //#include "Search.h"
  26.  
  27. struct TagItem frtags[] =
  28. {
  29.     ASLFR_TitleText,         (ULONG)"CycleDataBase File Requester",
  30.     ASLFR_InitialHeight,     MYHEIGHT,
  31.     ASLFR_InitialWidth,      MYWIDTH,
  32.     ASLFR_InitialLeftEdge,   MYLEFTEDGE,
  33.     ASLFR_InitialTopEdge,    MYTOPEDGE,
  34.     ASLFR_PositiveText,      (ULONG)"Okay",
  35.     ASLFR_NegativeText,      (ULONG)"Cancel",
  36.     ASLFR_InitialFile,       (ULONG)".cdb",
  37.     TAG_DONE
  38. };
  39.  
  40. /*
  41. ** Function for loading the database from a file.
  42. */
  43. // Code removed for copyright reasons
  44.  
  45. // Function creates gadgets in search window
  46. struct Gadget *createSearchGadgets(struct Gadget **glistptr, void *searchvi,
  47.     UWORD searchtopborder, struct Gadget *search_gads[], struct StringInfo *gad_ptr[])
  48. {
  49. struct NewGadget ng;
  50. struct Gadget *gad;
  51.  
  52. STRPTR names[] =
  53. {
  54.     "Destination",
  55.     "Date",
  56.     NULL,
  57. };
  58.  
  59. /* All the gadget creation calls accept a pointer to the previous gadget, and
  60. ** link the new gadget to that gadget's NextGadget field.  Also, they exit
  61. ** gracefully, returning NULL, if any previous gadget was NULL.  This limits
  62. ** the amount of checking for failure that is needed.  You only need to check
  63. ** before you tweak any gadget structure or use any of its fields, and finally
  64. ** once at the end, before you add the gadgets.
  65. */
  66.  
  67. /* The following operation is required of any program that uses GadTools.
  68. ** It gives the toolkit a place to stuff context data.
  69. */
  70. gad = CreateContext(glistptr);
  71.  
  72. /* Since the NewGadget structure is unmodified by any of the CreateGadget()
  73. ** calls, we need only change those fields which are different.
  74. */
  75.  
  76. ng.ng_LeftEdge   = 180;
  77. ng.ng_TopEdge    = 10+searchtopborder;
  78. ng.ng_Width      = 150;
  79. ng.ng_Height     = 12;
  80. ng.ng_GadgetText = "_Enter Search String:";
  81. ng.ng_TextAttr   = &Topaz80;
  82. ng.ng_VisualInfo = searchvi;
  83. ng.ng_GadgetID   = MYGAD_SEARCHSTRING;
  84. ng.ng_Flags      = NG_HIGHLABEL;
  85. search_gads[MYGAD_SEARCHSTRING] = gad = CreateGadget(STRING_KIND, gad, &ng,
  86.                     GTST_String,   "",
  87.                     GTST_MaxChars, 100,
  88.                     GT_Underscore, '_',
  89.                     TAG_END);
  90. gad_ptr[0] = (struct StringInfo *) gad->SpecialInfo;
  91.  
  92. ng.ng_LeftEdge   = 450;
  93. ng.ng_GadgetText = NULL;
  94. ng.ng_GadgetID   = MYGAD_DESTDATE;
  95. search_gads[MYGAD_DESTDATE] = gad = CreateGadget(MX_KIND, gad, &ng,
  96.                     GTMX_Active, 0,
  97.                     GTMX_Labels, names,
  98.                     GTMX_Spacing, 4,
  99.                     TAG_END);
  100.  
  101. return(gad);
  102. }
  103.  
  104. /*
  105. ** Function to handle a GADGETUP or GADGETDOWN event.
  106. */
  107. BOOL searchGadgetEvent(struct Screen *mysc, struct Window *searchwin, struct Gadget *gad, UWORD code,
  108.     struct Gadget *search_gads[], struct StringInfo *gad_ptr[], BOOL &destdate)
  109. {
  110. long state = 0;
  111.  
  112. switch (gad->GadgetID)
  113.     {
  114.     case MYGAD_SEARCHSTRING:
  115.         return(TRUE);
  116.         break;
  117.     case MYGAD_DESTDATE:
  118.         GT_GetGadgetAttrs(search_gads[MYGAD_DESTDATE], searchwin, NULL, GTMX_Active, &state, TAG_END);
  119.         if(state == 0)
  120.             destdate = FALSE;
  121.         else if(state == 1)
  122.             destdate = TRUE;
  123.         break;
  124.     }
  125.     return(FALSE);
  126. }
  127.  
  128. /*
  129. ** Function to handle vanilla keys.
  130. */
  131. void searchVanillaKey(struct Window *searchwin, UWORD code,
  132.     struct Gadget *search_gads[])
  133. {
  134. switch (code)
  135.     {
  136.     case 'e':
  137.     case 'E':
  138.         ActivateGadget(search_gads[MYGAD_SEARCHSTRING], searchwin, NULL);
  139.         break;
  140.     }
  141. }
  142.  
  143. /*
  144. ** Function for defining and displaying About Alert message.
  145. */
  146. void about_alert()
  147. {
  148.     char message[161];
  149.  
  150.     strcpy(message, "   Advanced Search program created by Donald W Millican.");
  151.     strcat(message,
  152.           "     (C) Copyright 1999 DWM Productions.");
  153.     strcat(message,
  154.           "     Version 1.0 26/09/99. Press Left Mouse Button to continue.");
  155.  
  156.     message[0]=0;       /* X position of the first string */
  157.     message[1]=32;      /*               - " -            */
  158.     message[2]=16;      /* Y             - " -            */
  159.  
  160.     message[56]='\0';   /* NULL sign which finish of the first string. */
  161.     message[57]=TRUE;   /* Continuation byte set to TRUE (new string). */
  162.  
  163.     message[58]=0;
  164.     message[59]=32;
  165.     message[60]=32;
  166.  
  167.     message[96]='\0';
  168.     message[97]=TRUE;
  169.  
  170.     message[98]=0;      /* X position of the third string. */
  171.     message[99]=32;     /*               - " -             */
  172.     message[100]=48;     /* Y             - " -             */
  173.  
  174.     message[159]='\0';  /* NULL sign which finish of the third string.   */
  175.     message[160]=FALSE; /* Continuation byte set to FALSE (last string). */
  176.  
  177.     /* We will now display the Alert message: */
  178.     DisplayAlert( RECOVERY_ALERT, message, 64 );
  179. }
  180.  
  181. // Function to deal with user input
  182. void eventhandling(struct Screen *mysc, struct Window *searchwin,
  183.     struct Gadget *search_gads[], struct Menu *mainmenustrip, struct StringInfo *gad_ptr[])
  184. {
  185.     struct IntuiMessage *imsg;
  186.     ULONG imsgClass;
  187.     UWORD imsgCode;
  188.     UWORD menu_number, which_menu, which_item;
  189.     MenuItem *item;
  190.     BOOL terminated = FALSE;
  191.     BOOL destdate = FALSE;
  192.     BOOL result = FALSE;
  193.     struct Gadget *gad;
  194.     int count = 0;
  195.     char sentence[DESTLENGTH];
  196.     UBYTE *data_entered;
  197.     int position = 0;
  198.  
  199.     data_entered = (UBYTE *)sentence;
  200.  
  201.     // Load cycledbase file
  202.     result = load(searchwin);
  203.     if(result == 0)
  204.     {
  205.         terminated = TRUE;
  206.     }
  207.  
  208. while (!terminated)
  209.     {
  210.     Wait (1 << searchwin->UserPort->mp_SigBit);
  211.  
  212.     /* GT_GetIMsg() returns an IntuiMessage with more friendly information for
  213.     ** complex gadget classes.  Use it wherever you get IntuiMessages where
  214.     ** using GadTools gadgets.
  215.     */
  216.  
  217.     while ((!terminated) &&
  218.            (imsg = GT_GetIMsg(searchwin->UserPort)))
  219.         {
  220.         /* Presuming a gadget, of course, but no harm...
  221.         ** Only dereference this value (gad) where the Class specifies
  222.         ** that it is a gadget event.
  223.         */
  224.         gad = (struct Gadget *)imsg->IAddress;
  225.  
  226.         imsgClass = imsg->Class;
  227.         imsgCode = imsg->Code;
  228.         menu_number = imsg->Code;
  229.  
  230.         /* Use the toolkit message-replying function here... */
  231.         GT_ReplyIMsg(imsg);
  232.  
  233.         switch (imsgClass)
  234.             {
  235.             /*  --- WARNING --- WARNING --- WARNING --- WARNING --- WARNING ---
  236.             ** GadTools puts the gadget address into IAddress of IDCMP_MOUSEMOVE
  237.             ** messages.  This is NOT true for standard Intuition messages,
  238.             ** but is an added feature of GadTools.
  239.             */
  240.             case IDCMP_VANILLAKEY:
  241.                 searchVanillaKey(searchwin, imsgCode, search_gads);
  242.                 break;
  243.  
  244.             case IDCMP_GADGETDOWN:
  245.             case IDCMP_GADGETUP:
  246.                 // terminated should not be used here
  247.                 // we want the program to continue running
  248.                 // after something has been entered
  249.                 result = searchGadgetEvent(mysc, searchwin, gad, imsgCode, search_gads,
  250.                                 gad_ptr, destdate);
  251.                 if(result == TRUE)
  252.                 {
  253.                     searchrecords.recs[0] = 0;
  254.                     // Copy out contents of string gadget
  255.                     strcpy(data_entered, gad_ptr[0]->Buffer);
  256.  
  257.             // Perform search
  258.             
  259.             // Code removed for copyright reasons
  260.                 }
  261.                 break;
  262.  
  263.             case IDCMP_MENUPICK:
  264.                 // Menu chosen
  265.                 which_menu=MENUNUM(menu_number);
  266.                 which_item=ITEMNUM(menu_number);
  267.  
  268.                 while( menu_number != MENUNULL )
  269.                 {
  270.                     /* Get the address of the item: */
  271.                     item = (struct MenuItem *) ItemAddress( mainmenustrip, menu_number );
  272.  
  273.                     /* Check which item was selected: */
  274.                     if( which_menu == 0 )
  275.                     {
  276.                         if( which_item == 0 )
  277.                         {
  278.                             // about selected
  279.                             about_alert();
  280.                         }
  281.                         if( which_item == 1 )
  282.                         {
  283.                             terminated = TRUE;
  284.                         }
  285.                     }
  286.                     /* Get the following item's menu number: */
  287.                     menu_number = item->NextSelect;
  288.                 }
  289.                 break;
  290.  
  291.             case IDCMP_CLOSEWINDOW:
  292.                 terminated = TRUE;
  293.                 break;
  294.  
  295.             case IDCMP_REFRESHWINDOW:
  296.                 /* With GadTools, the application must use GT_BeginRefresh()
  297.                 ** where it would normally have used BeginRefresh()
  298.                 */
  299.                 GT_BeginRefresh(searchwin);
  300.                 GT_EndRefresh(searchwin, TRUE);
  301.                 break;
  302.             }
  303.          }
  304.     }
  305. }
  306.  
  307. // Function to create and open search window
  308. void
  309. searchwindow()
  310. {
  311.     struct Window   *searchwin;
  312.     struct TextFont *font;
  313.     struct Screen   *mysc;
  314.     struct Gadget   *searchglist, *search_gads[NO_GADS];
  315.     struct StringInfo *gad_ptr[PTRS];
  316.     void            *searchvi;
  317.     APTR            searchvisualinfo;
  318.     UWORD           searchtopborder;
  319.     struct Menu *mainmenustrip;
  320.  
  321. /****************************/
  322. /* THE MENU STRUCTURE: */
  323. /****************************/
  324. struct NewMenu main_menu[] =
  325. {
  326. {NM_TITLE,"Main",0,0,0,0,},
  327. {NM_ITEM,"About","A",0,0,0,},
  328. {NM_ITEM,"Quit","Q",0,0,0,},
  329. {NM_END,NULL,0,0,0,0,},
  330. };
  331.  
  332. // Copy file identification string to header structure
  333. strcpy(headerdata.file_id, FILEDEF);
  334. // Set number of actual records to zero
  335. records.recs[0] = 0;
  336.  
  337. /* Open topaz 8 font, so we can be sure it's openable
  338. ** when we later set ng_TextAttr to &Topaz80:
  339. */
  340.  
  341. if (NULL == (font = OpenFont(&Topaz80)))
  342.     {
  343.         EasyRequest(NULL, &nofont, NULL);
  344.     }
  345. else
  346.     {
  347.     if (NULL == (mysc = LockPubScreen(NULL)))
  348.         {
  349.             EasyRequest(NULL, &noscreen, NULL);
  350.         }
  351.     else
  352.         {
  353.         if (NULL == (searchvi = GetVisualInfo(mysc, TAG_END)))
  354.             {
  355.                 EasyRequest(NULL, &novisinfo, NULL);
  356.             }
  357.         else
  358.             {
  359.             /* Here is how we can figure out ahead of time how tall the  */
  360.             /* window's title bar will be:                               */
  361.             searchtopborder = mysc->WBorTop + (mysc->Font->ta_YSize + 1);
  362.  
  363.             if (NULL == createSearchGadgets(&searchglist, searchvi, searchtopborder,
  364.                                          search_gads, gad_ptr))
  365.                 {
  366.                     EasyRequest(NULL, &nogadgets, NULL);
  367.                 }
  368.             else
  369.                 {
  370.  
  371.         if (NULL == (searchwin = OpenWindowTags(NULL,
  372.             WA_Title,     "Advanced Search",
  373.             WA_Gadgets,   searchglist, WA_AutoAdjust,    TRUE,
  374.             WA_Width,       490,       WA_MinWidth,      490,
  375.             WA_InnerHeight, 50,        WA_MinHeight,     50,
  376.             WA_DragBar,    TRUE,       WA_DepthGadget,   TRUE,
  377.             WA_Activate,   TRUE,       WA_CloseGadget,   TRUE,
  378.             WA_SimpleRefresh, TRUE,    WA_NewLookMenus, TRUE,
  379.             WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW | IDCMP_MENUPICK |
  380.                       IDCMP_VANILLAKEY | IDCMP_GADGETDOWN | STRINGIDCMP,
  381.             WA_PubScreen, mysc,
  382.             TAG_END)))
  383.                     {
  384.                         EasyRequest(NULL, &nowindow, NULL);
  385.                     }
  386.                 else
  387.                     {
  388.                     /* After window is open, gadgets must be refreshed with a
  389.                     ** call to the GadTools refresh window function.
  390.                     */
  391.                     GT_RefreshWindow(searchwin, NULL);
  392.  
  393.                     searchvisualinfo=GetVisualInfo(mysc, TAG_END);
  394.                     mainmenustrip=CreateMenus(main_menu, TAG_END);
  395.                     LayoutMenus(mainmenustrip, searchvisualinfo, GTMN_NewLookMenus, TRUE, TAG_END);
  396.                     SetMenuStrip(searchwin, mainmenustrip);
  397.  
  398.                     // Deal with interaction
  399.                     eventhandling(mysc, searchwin, search_gads, mainmenustrip, gad_ptr);
  400.  
  401.                     ClearMenuStrip( searchwin );
  402.                     FreeMenus(mainmenustrip);
  403.  
  404.                     CloseWindow(searchwin);
  405.                     }
  406.                 }
  407.             /* FreeGadgets() even if createAllGadgets() fails, as some
  408.             ** of the gadgets may have been created...If glist is NULL
  409.             ** then FreeGadgets() will do nothing.
  410.             */
  411.             FreeGadgets(searchglist);
  412.             FreeVisualInfo(searchvi);
  413.             FreeVisualInfo(searchvisualinfo);
  414.             }
  415.         UnlockPubScreen(NULL, mysc);
  416.         }
  417.     CloseFont(font);
  418.     }
  419. }
  420.  
  421.